<?php
class News_IndexController extends XPHP_Controller
{
	public function _init()
	{
		$this->loadLayout('/VietTrung_Detail');
	}

    /**
     * Lấy ra các danh sách tin tức
     * @return void
     */
    public function indexAction()
    {
        $model = new Areas_News_Models_News();
        $newsest = $model->getLastestNews(8);
        //Lay ra danh sach cac danh muc cua tin tuc
        foreach($newsest as $k => $n)
        {
            $modelNC = new Areas_News_Models_NewsCategory($n->category_id);
            $newsest[$k]->category_name = $modelNC->name;
        }
        $this->view->news = $newsest;
        $this->loadLayout('/VietTrung_Home');
        return $this->view();
    }
	
	public function categoryAction()
	{
		//Lấy danh sách tin tức thuộc danh mục + danh mục con.
		$modelNewsCat = new Areas_News_Models_NewsCategory();
		$modelNews = new Areas_News_Models_News();
		$arrChildCat = $modelNewsCat->getCategoryTree($this->params['id']);
		$modelNews->db->where('category_id', $this->params['id']);
		foreach ($arrChildCat as $childCat)
		{
			$modelNews->db->or_where('category_id', $childCat->id);
		}
		$arrNews = $modelNews->db->order_by('date_create', 'DESC')
								->get()
								->result();
		$category = $modelNewsCat->db->where('id', $this->params['id'])
											->get()
											->result();
		$this->view->category = $category[0];
		$this->view->title = $category[0]->title;
		$this->view->arrNews = $arrNews;
		return $this->view();
	}
	
	public function detailAction()
	{
		//Lấy tin tức
		$modelNews = new Areas_News_Models_News();
		$news = $modelNews->db->where('id', $this->params['id'])
							->get()
							->result();
		$this->view->news = $news[0];
		$this->view->title = $news[0]->title;
		return $this->view();
	}
}